package com.tencent.cloud.iov.common.flow.view.impl;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.tencent.cloud.iov.common.flow.view.ILoadingFlowView;
import com.tencent.cloud.uikit.ui.widget.IovLoadingView;

/**
 * 加载动画组件默认实现
 *
 * @author showxiao
 * @date 2018/10/10
 */
public final class DefaultLoadingFlowView implements ILoadingFlowView {

    private final ILoadingFlowView mActualFlowView;

    private FrameLayout mContentContainer;
    private View mContentView;
    private IovLoadingView mLoadingView;

    public DefaultLoadingFlowView(@NonNull ILoadingFlowView contentViewContainer) {
        mActualFlowView = contentViewContainer;
    }

    @Override
    public void setContentView(int resId) {
        ensureContentContainer();

        View contentView = LayoutInflater.from(getContext()).inflate(
                resId, mContentContainer, false
        );
        setContentView(contentView);
    }

    @Override
    public void setContentView(View contentView) {
        setContentView(contentView, new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT
        ));
    }

    @Override
    public void setContentView(View contentView, ViewGroup.LayoutParams params) {
        if (mContentContainer != null) {

            if (mContentContainer.indexOfChild(mContentView) != -1) {
                mContentContainer.removeView(mContentView);
            }

            if (mContentContainer.indexOfChild(contentView) == -1) {
                mContentContainer.addView(contentView);
            }

            if (mLoadingView != null) {
                mLoadingView.bringToFront();
            }

            mActualFlowView.superSetContentView(mContentContainer, params);
        } else {
            mActualFlowView.superSetContentView(contentView, params);
        }

        mContentView = contentView;
    }

    @Override
    public void superSetContentView(View contentView, ViewGroup.LayoutParams params) {
        // never used
    }

    @Override
    public void showLoadingView() {
        showLoadingView(null);
    }

    @Override
    public void showLoadingView(CharSequence text) {
        if (mContentContainer == null) {
            ensureContentContainer();

            // attach content view to content container
            if (mContentView != null) {
                ViewGroup parent = (ViewGroup) mContentView.getParent();
                parent.removeView(mContentView);
                parent.addView(mContentContainer);
                mContentContainer.addView(mContentView);
            }
        }

        ensureLoadingView();

        mLoadingView.setVisibility(View.VISIBLE);
        mLoadingView.setText(text);
    }

    @Override
    public void hideLoadingView() {
        if (mLoadingView != null) {
            mLoadingView.setVisibility(View.GONE);
        }
    }

    @Override
    public IovLoadingView createLoadingView() {
        return mActualFlowView.createLoadingView();
    }

    private void ensureContentContainer() {
        if (mContentContainer == null) {
            mContentContainer = new FrameLayout(getContext());
            mContentContainer.setLayoutParams(new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT
            ));
        }
    }

    private void ensureLoadingView() {
        if (mLoadingView == null) {
            mLoadingView = createLoadingView();
            mLoadingView.setLayoutParams(new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT
            ));
            mContentContainer.addView(mLoadingView);
        }
    }

    @Override
    public Context getContext() {
        return mActualFlowView.getContext();
    }
}
